package com.sdleyou.treepro.business.api.mission;

import com.sdleyou.treepro.common.model.utils.ResultVOUtil;
import com.sdleyou.treepro.common.model.vo.ResultVO;
import com.sdleyou.treepro.common.rabbitmq.AmqpExchange;
import com.sdleyou.treepro.common.rabbitmq.message.StepChangeMsg;
import com.sdleyou.treepro.common.service.MissionManager;
import com.sdleyou.treepro.common.service.UserManager;
import com.sdleyou.treepro.common.service.UserMissionManager;
import com.sdleyou.treepro.common.utils.DateUtil;
import com.sdleyou.treepro.system.version.ApiVersion;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/api")
public class MissionController {

    @Autowired
    private UserMissionManager missionManager;
    @Autowired
    private UserManager userManager;
    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 获取用户任务完成情况
     */
    @ApiVersion(1)
    @PostMapping("/{version}/mission/info")
    public ResultVO userInfo(@RequestParam("user_id") String userId){
        return ResultVOUtil.success(missionManager.getMissionInfo(userId));
    }

    /**
     * 完成任务接口
     * 任务类型 sign-签到任务 step-捐赠步数任务 water-浇水任务 fertilizer-施肥任务 pest-除虫任务 adv-广告任务 share-分享任务 invent-邀请好友任务
     * recall-好友召回任务 feed-给小助手喂食任务 signature-生成日签任务 noon-中午登陆查看任务
     */
    @ApiVersion(1)
    @PostMapping("/{version}/mission/finish")
    public ResultVO finishMission(@RequestParam("user_id") String userId,
                                  @NotNull @RequestParam("type") String type,
                                  @RequestParam(name="multiple",defaultValue = "1") Integer multiple){

        return ResultVOUtil.success(missionManager.finishMission(userId, type, multiple));
    }

    /**
     * 增加分享任务次数
     */
    @ApiVersion(1)
    @PostMapping("/{version}/mission/share")
    public ResultVO updateShare(@RequestParam("user_id") String userId){
        return ResultVOUtil.success(missionManager.updateMission(userId, "share", null));
    }

    /**
     * 增加看视频任务次数
     */
    @ApiVersion(1)
    @PostMapping("/{version}/mission/adv")
    public ResultVO updateAdv(@RequestParam("user_id") String userId){
        return ResultVOUtil.success(missionManager.updateMission(userId, "adv", null));
    }

    /**
     * 更新日签任务状态
     */
    @ApiVersion(1)
    @PostMapping("/{version}/mission/signature")
    public ResultVO updateSignature(@RequestParam("user_id") String userId){
        return ResultVOUtil.success(missionManager.updateMission(userId, "signature", null));
    }

    /**
     * 更新任务状态
     * @return
     */
    @ApiVersion(1)
    @PostMapping("/{version}/mission/update/condition")
    public ResultVO updateCondition() {
        //missionManager.updateMission("1", "sign", 1);
        //missionManager.updateMission();
        //missionManager.updateAll();
        return ResultVOUtil.success();
    }

    /**
     * 更新任务信息
     * @param userId
     * @return
     */
    @ApiVersion(1)
    @PostMapping("/{version}/mission/update/info")
    public ResultVO updateInfo(@RequestParam("user_id") String userId) {
        //missionManager.update(userId);
        return ResultVOUtil.success();
    }

    /**
     * 生成日签图片接口
     */
    @ApiVersion(1)
    @PostMapping("/{version}/mission/createSignature")
    public ResultVO createSignature(@RequestParam("user_id") String userId){
        return ResultVOUtil.success(userManager.getTodayLot(userId));
    }

    /**
     * 查询首次登陆奖励情况
     */
    @ApiVersion(1)
    @PostMapping("/{version}/mission/getFirstLoginInfo")
    public ResultVO getFirstLoginInfo(@RequestParam("user_id") String userId){
        return ResultVOUtil.success(missionManager.getFirstLoginInfo(userId));
    }
}
